package com.docmanage.am.api;

import com.alibaba.fastjson2.JSON;
import com.docmanage.am.domain.dto.GtserverApiDTO;
import com.docmanage.am.domain.dto.ParaApiDto;
import com.docmanage.am.domain.vo.GtserverApiVo;
import com.docmanage.common.config.ApiProperties;
import com.docmanage.common.enums.KeyInfoEnum;
import com.docmanage.common.utils.DateUtils;
import com.docmanage.common.utils.sign.Base64;
import com.docmanage.common.utils.sign.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author yhd
 * @create 2024/1/25 10:24
 */
@Service
@Slf4j
public class RemoteApiClient {

    @Resource
    private ApiProperties apiProperties;

    @Resource
    private RestTemplate restTemplate;

    private static String URL="http://13.11.100.141:8089/GTSERVER/api/Script/Post/5870fb90-109f-4bc1-8ea9-77599ea021d2";


    private HttpHeaders getHttpHeaders(){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);

        return httpHeaders;
    }

public List<GtserverApiVo> getApiVO(ParaApiDto dto){
    GtserverApiDTO gtserverApiDTO = new GtserverApiDTO();
    gtserverApiDTO.setToken(apiProperties.getToken());
    gtserverApiDTO.setQxdm(dto.getQxdm());
    gtserverApiDTO.setKey(KeyInfoEnum.GETYWJD.getKey());
    if (ObjectUtils.isEmpty(dto)){
        return null;
    }
    String dtoStr = JSON.toJSONString(dto);
    //两次base64转两次
    dtoStr=Base64.encode(dtoStr.getBytes(StandardCharsets.UTF_8));
    dtoStr=Base64.encode(dtoStr.getBytes(StandardCharsets.UTF_8));
    log.info("dtoStr:{},",dtoStr);
    gtserverApiDTO.setPara(dtoStr);
    String cert=Md5Utils.hash(gtserverApiDTO.getToken().concat(gtserverApiDTO.getPara()).concat(DateUtils.getDate()));
    gtserverApiDTO.setCert(cert);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity httpEntity = new HttpEntity<>(headers);
    ResponseEntity<List> response=restTemplate.exchange(URL, HttpMethod.POST,httpEntity,List.class);
    log.info("List:{},",response);
    List<GtserverApiVo> list = response.getBody();
    return list;
}
}
